﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text.RegularExpressions;
using Telerik.Web.UI;

public partial class Category_UserControl_uc_Cate_Route_Detail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
        }
       LoadDataFromddlTinhThanh();
    }
    private void InitData()
    {
        ClearTextBox();
        ZD_CATE_ROUTE_BELONG objRouteBeLong = new ZD_CATE_ROUTE_BELONG();
        objRouteBeLong.KHOID = "";
        objRouteBeLong.VUNGID = "";
        objRouteBeLong.TINHTHANHID = "";
        objRouteBeLong.QUANHUYENID = "";
        DisplayInGrid(objRouteBeLong);
    }

    private void ClearTextBox()
    {
        GetAllRoute();
        GetAllKho("");
        GetAllVung("");
        GetAllTinhThanh("");
        GetAllQuanHuyen("");
        hdfID.Value = "-1";
        hdfQUANHUYENID.Value = "";
        hdfTINHTHANHID.Value = "";
        ddlKho.SelectedValue = "";
        btnSave.Text = "Thêm mới";
        btnSave.Enabled = true;
        lblAlerting.Text = string.Empty;
    }
    protected void DisplayInGrid(ZD_CATE_ROUTE_BELONG objRouteBelong)
    {
        try
        {
            string DSTINHTHANHID = GetItemOfDDL(ddlTinhThanh);
            string DSQUANHUYENID = GetItemOfDDL(ddlQuanHuyen);

            CateRouteBO ct = new CateRouteBO();
            List<ZD_PRC_ZD_CATE_ROUTE_BELONG_SEARCHResult> lst = new List<ZD_PRC_ZD_CATE_ROUTE_BELONG_SEARCHResult>();
            lst = ct.CateRoute_Belong_Search(objRouteBelong, DSTINHTHANHID, DSQUANHUYENID).ToList();
            grdRouteList.DataSource = lst;
            if (lst.Count > 0)
            {
                grdRouteList.PageIndex = 0;
            }
            grdRouteList.DataBind();

        }
        catch (Exception)
        {

            throw;
        }

    }
    private void GetAllRoute()
    {

        try
        {
            List<ZD_PRC_ZD_CATE_ROUTE_GETALLResult> lst = new List<ZD_PRC_ZD_CATE_ROUTE_GETALLResult>();
            CateRouteBO ct = new CateRouteBO();
            lst = ct.CateRouteGetAll();
            if (lst != null)
            {
                ddlRoute.DataSource = lst;
                ddlRoute.DataTextField = "ROUTENAME";
                ddlRoute.DataValueField = "ROUTEID";
                ddlRoute.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "-1");
                ddlRoute.Items.Insert(0, lstParent);
                ddlRoute.SelectedIndex = ddlRoute.Items.IndexOf(lstParent);
            }
        }
        catch (Exception)
        {


        }
    }

    private void GetAllKho(string id)
    {
        try
        {
            CateRouteBO ct = new CateRouteBO();
            List<DAL.ZD_PRC_V_ZD_GET_LIST_WAREHOUSE_GETBYIDResult> lst = new List<DAL.ZD_PRC_V_ZD_GET_LIST_WAREHOUSE_GETBYIDResult>();
            lst = ct.KhoGet(id).ToList();
            if (lst != null)
            {
                ddlKho.DataSource = lst;
                ddlKho.DataTextField = "TENKHO";
                ddlKho.DataValueField = "KHOID";
                ddlKho.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "");
                ddlKho.Items.Insert(0, lstParent);
                ddlKho.SelectedIndex = ddlKho.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }

    private void GetAllVung(string id)
    {
        try
        {
            if (id != null && id.Equals(""))
            {
                CateRouteBO ct = new CateRouteBO();
                List<DAL.ZD_PRC_ZD_REGION_GETBYIDResult> lst = new List<DAL.ZD_PRC_ZD_REGION_GETBYIDResult>();
                lst = ct.VungGet(id).ToList();
                if (lst != null)
                {
                    ddlVung.DataSource = lst;
                    ddlVung.DataTextField = "TENVUNG";
                    ddlVung.DataValueField = "VUNGID";
                    ddlVung.DataBind();

                    ListItem lstParent = new ListItem("--Chọn--", "");
                    ddlVung.Items.Insert(0, lstParent);
                    ddlVung.SelectedIndex = ddlVung.Items.IndexOf(lstParent);
                }
            }
            else
            {
                ddlVung.DataSource = null;
                ddlVung.DataBind();
            }
        }
        catch
        {
        }
    }

    private void GetAllTinhThanh(string id)
    {
        try
        {
            List<DAL.ZD_PRC_V_ZD_GET_LIST_PROVINCE_GETBYIDResult> lst = new List<DAL.ZD_PRC_V_ZD_GET_LIST_PROVINCE_GETBYIDResult>();
            if (id != null && !id.Equals(""))
            {
                CateRouteBO ct = new CateRouteBO();
                lst = ct.TinhThanhGet(id).ToList();
                if (lst != null)
                {
                    ddlTinhThanh.DataSource = lst;
                    ddlTinhThanh.DataTextField = "TENTINHTHANH";
                    ddlTinhThanh.DataValueField = "TINHTHANHID";
                    ddlTinhThanh.DataBind();
                    foreach (RadComboBoxItem item in ddlTinhThanh.Items)
                    {
                        item.Checked = true;
                    }
                }
            }
            else
            {
                ddlTinhThanh.DataSource = lst;
                ddlTinhThanh.DataBind();
            }
        }
        catch
        {
        }
    }

    private void GetAllQuanHuyen(string id)
    {
        try
        {
            List<DAL.ZD_PRC_V_ZD_GET_LIST_DISTRICT_GETBY_TINHTHANHIDResult> lst = new List<DAL.ZD_PRC_V_ZD_GET_LIST_DISTRICT_GETBY_TINHTHANHIDResult>();
            if (!hdfTINHTHANHID.Value.Equals(id))
            {
                hdfTINHTHANHID.Value = id;
                if (id != null && !id.Equals(""))
                {
                    CateRouteBO ct = new CateRouteBO();
                    lst = ct.QuanHuyenGet(id).ToList();
                    if (lst != null)
                    {
                        ddlQuanHuyen.DataSource = lst;
                        ddlQuanHuyen.DataTextField = "TENQUANHUYEN";
                        ddlQuanHuyen.DataValueField = "QUANHUYENID";
                        ddlQuanHuyen.DataBind();
                        foreach (RadComboBoxItem item in ddlQuanHuyen.Items)
                        {
                            if (hdfQUANHUYENID.Value.Equals(""))
                            {
                                item.Checked = true;
                            }
                            else
                            {
                                if (item.Value == hdfQUANHUYENID.Value)
                                {
                                    item.Checked = true;
                                }
                                else
                                {
                                    item.Checked = false;
                                }
                            }
                        }

                    }
                }
                else
                {
                    ddlQuanHuyen.DataSource = lst;
                    ddlQuanHuyen.DataBind();
                }
            }
        }
        catch
        {
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hdfID.Value = "-1";
        hdfQUANHUYENID.Value = "";
        btnSave.Text = "Thêm mới";
        lblAlerting.Text = string.Empty;
        LoadGrid();
    }

    protected void btnXoaTrang_Click(object sender, EventArgs e)
    {
        ClearTextBox();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        hdfQUANHUYENID.Value = "";
        if (int.Parse(ddlRoute.SelectedValue) <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn tuyến!";
            return;
        }

        if (ddlKho.SelectedValue.Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn kho!";
            return;
        }
        if (ddlVung.SelectedValue.Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn vùng!";
            return;
        }
        if (ddlTinhThanh.CheckedItems.Count <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn tỉnh thành!";
            return;
        }
        if (ddlQuanHuyen.CheckedItems.Count <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn quận huyện!";
            return;
        }

        // Thuc hien Insert Update
        ZD_CATE_ROUTE_BELONG objRouteBeLong = new ZD_CATE_ROUTE_BELONG();
        objRouteBeLong.ID = int.Parse(hdfID.Value);
        objRouteBeLong.ROUTEID = int.Parse(ddlRoute.SelectedValue);
        objRouteBeLong.KHOID = ddlKho.SelectedValue;
        objRouteBeLong.VUNGID = ddlVung.SelectedValue;
        
        CateRouteBO ct = new CateRouteBO();
        if (int.Parse(hdfID.Value) <= 0)
        {
            int rs = ct.CateRoute_Belong_Insert(objRouteBeLong, GetItemOfDDL(ddlTinhThanh), GetItemOfDDL(ddlQuanHuyen));
            if (rs > 0)
            {
                btnSave.Text = "Cập nhật";
                if (ddlQuanHuyen.CheckedItems.Count > 1)
                {
                    btnSave.Enabled = false;
                }
                lblAlerting.Text = "Thêm mới tuyến thành công!";
            }
            else
            {
                lblAlerting.Text = "Thêm mới tuyến thất bại, bạn vui lòng thử lại!";
            }
        }
        else
        {
            if (ddlTinhThanh.CheckedItems.Count > 1)
            {
                lblAlerting.Text = "Bạn không thể cập nhật tuyến này!";
                return;
            }
            else
            {
                string result = "";
                if (ddlTinhThanh.CheckedItems.Count > 0)
                {
                    foreach (RadComboBoxItem item in ddlTinhThanh.CheckedItems)
                    {
                        result = item.Value;
                        break;
                    }

                }
                objRouteBeLong.TINHTHANHID = result;
            }
            if (ddlQuanHuyen.CheckedItems.Count > 1)
            {
                lblAlerting.Text = "Bạn không thể cập nhật tuyến này!";
                return;
            }
            else
            {
                string result = "";
                if (ddlQuanHuyen.CheckedItems.Count > 0)
                {
                    foreach (RadComboBoxItem item in ddlQuanHuyen.CheckedItems)
                    {
                        result = item.Value;
                        break;
                    }

                }
                objRouteBeLong.QUANHUYENID = result;
            }
           
            if (ct.CateRoute_Belong_Update(objRouteBeLong))
            {
                //btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Cập nhật tuyến thành công!";
            }
            else
            {
                lblAlerting.Text = "Cập nhật tuyến thất bại, bạn vui lòng thử lại!";
            }
        }
        LoadGrid();

    }
    private void LoadGrid()
    {
        ZD_CATE_ROUTE_BELONG objRouteBeLong = new ZD_CATE_ROUTE_BELONG();
        objRouteBeLong.ROUTEID = int.Parse(ddlRoute.SelectedValue);
        objRouteBeLong.KHOID = ddlKho.SelectedValue;
        objRouteBeLong.VUNGID = ddlVung.SelectedValue;
        DisplayInGrid(objRouteBeLong);
    }


    protected void ddlVung_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlVung.SelectedValue.Length > 0)
        {
            GetAllTinhThanh(ddlVung.SelectedValue);
        }
        else
        {
            GetAllTinhThanh("");
        }
        LoadDataFromddlTinhThanh();

    }
    protected void grdRouteList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadGrid();
        grdRouteList.PageIndex = e.NewPageIndex;
        grdRouteList.DataBind();
    }
    protected void grdRouteList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdRouteList_RowEditing(object sender, GridViewEditEventArgs e)
    {

        lblAlerting.Text = string.Empty;
        btnSave.Text = "Cập nhật";
        grdRouteList.EditIndex = e.NewEditIndex;
        hdfID.Value = grdRouteList.DataKeys[e.NewEditIndex].Value.ToString();
        string strRouteID = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingRouteID")).Text;
        string strKhoID = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingKhoID")).Text;
        string strVungID = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingVungID")).Text;
        string strTinhThanhID = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingTinhThanhID")).Text;
        string strQuanHuyenID = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingQuanHuyenID")).Text;
        string strLeadTime = ((Label)grdRouteList.Rows[e.NewEditIndex].FindControl("lblListingLeadTime")).Text;
       
        // Bind len control
        ddlRoute.SelectedValue = strRouteID;
        ddlKho.SelectedValue = strKhoID;
        ddlVung.SelectedValue = strVungID;
        GetAllTinhThanh(strVungID);
        foreach (RadComboBoxItem item in ddlTinhThanh.Items)
        {
            if (item.Value == strTinhThanhID)
            {
                item.Checked = true;
            }
            else
            {
                item.Checked = false;
            }

        }

        GetAllQuanHuyen(strTinhThanhID + "@");
        foreach (RadComboBoxItem item in ddlQuanHuyen.Items)
        {
            if (item.Value == strQuanHuyenID)
            {
                item.Checked = true;
            }
            else
            {
                item.Checked = false;
            }

        }
        hdfQUANHUYENID.Value = strQuanHuyenID;
    }

    private string GetItemOfDDL(RadComboBox rbb)
    {
        string result = "";
        if (rbb.CheckedItems.Count > 0)
        {
            foreach (RadComboBoxItem item in rbb.CheckedItems)
            {
                result += item.Value + "@";
            }

        }
        return result;
    }

    private void LoadDataFromddlTinhThanh()
    {

        GetAllQuanHuyen(GetItemOfDDL(ddlTinhThanh));

    }
}